1
Beyond Basic Drawing: Composite Operations and Spray Tools
AI020 Lesson 10
00:00

This module transitions from static paths to dynamic, event-driven tools by manipulating the CanvasRenderingContext2D state. We focus on extending application capabilities through higher-order patterns and mathematical filtering.

1. Higher-Order Tool Patterns

Tools like tools.Line abstract mouse events using helpers like trackDrag. Conversely, tools.Spray implements interval-based repetition via setInterval, allowing continuous pixel placement while the mouse is held down.

2. Mathematical Constraints

A simple random offset creates a square distribution. To achieve a natural circular "brush" effect, we use the Pythagorean theorem to filter points: $x^2 + y^2 < r^2$. Alternatively, one could use polar coordinates with Math.sin and Math.cos for conversion.

3. Composite Operations & State

Beyond standard strokes, the globalCompositeOperation property defines how new pixels interact with existing ones. For example, setting it to 'destination-out' allows the tools.Erase function to turn pixels transparent rather than just painting them white. UI controls like controls.color and controls.brushSize provide real-time updates to fillStyle and lineWidth respectively.

The lineCap property (e.g., 'round' or 'square') controls the shape of the ends of stroked lines, critical for smooth drawing.
main.py
TERMINAL bash — 80x24
> Ready. Click "Run" to execute.
>